package com.nursinghome.user.controller;

import com.nursinghome.admin.service.AccountService;
import com.nursinghome.bean.Account;
import com.nursinghome.bean.RespBean;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.user.service.UserAccountService;
import com.nursinghome.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user/service")
public class UserServiceController {
    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private AccountService accountService;

    @GetMapping
    public RespBean selectByadata(@RequestHeader(value = "token", required = false) String token,
                                  String data) {
        //时间转换
        if (data.length() > 0 && data != null) {
            ZonedDateTime zdt = ZonedDateTime.parse(data);
            // 加上八小时
            ZonedDateTime newZdt = zdt.plusHours(8);
            // 转换为字符串
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
            data = newZdt.format(formatter);
        }
        //判断令牌
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer eidInt = (Integer) map.get("eid");
        Long  eid = new Long(eidInt + "");
        if (eid == null) {
            return RespBean.error("令牌信息不正确");
        }
        List<Account> account= userAccountService.selectByadata(data,eid);
        return RespBean.ok("",account);
    }
    @PutMapping("/{aid}")
    public RespBean updateAccountElderly(@PathVariable("aid") Long aid) throws NurisingHomeException {
        userAccountService.updateAccountElderly(aid);

        return RespBean.ok("支付成功");
    }
    @GetMapping("/byaid")
    public RespBean selectByAidByUser(Long aid){
        Account account = accountService.selectByAid(aid);

        return RespBean.ok("",account);
    }
}
